//
//  SettingViewModel.swift
//  Chatup
//
//  Created by op on 2023/10/17.
//

import Foundation
import StoreKit
import UIKit
import FirebaseAnalytics

struct SettingItemsModel:Identifiable {
  let id = UUID()
  var name:String
  var icon:String
  init(name: String, icon: String) {
    self.name = name
    self.icon = icon
  }
}

@MainActor
class SettingViewModel:ObservableObject{
    @Published var items:[SettingItemsModel] = []
    @Published var showVip = false
    @Published var isShowingShareView = false
    @Published var switchVIP = false {didSet{handleSetting()}}

  init() {
      items.append(.init(name: String.i18nText("settings_share"), icon: "icon_setting_share"))
      items.append(.init(name: String.i18nText("settings_rate"), icon: "icon_setting_rate"))
      items.append(.init(name: String.i18nText("settings_unlock"), icon: "icon_setting_vip"))
      items.append(.init(name: String.i18nText("privacy_policy"), icon: "icon_setting_privacy"))
      items.append(.init(name: String.i18nText("terms_of_use"), icon: "icon_setting_user"))
      logShowSetting()
  }
    
    func clickItem(index: Int){
        switch index {
        case 0:
            print("share")
            isShowingShareView = true
            logShareClick()
        case 1:
            print("Rate")
            if #available(iOS 14.0, *) {
                if let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene {
                    SKStoreReviewController.requestReview(in: scene)
                }
            } else {
                SKStoreReviewController.requestReview()
            }
            logRateClick()
        case 2:
            print("vip")
            showVip = true
        case 3:
            print("Privacy")
            UIApplication.openWebUrl(urlStr:"https://aichattings.com/privacy-policy/")
        case 4:
            print("Terms")
            UIApplication.openWebUrl(urlStr:"https://aichattings.com/terms-of-use/")
        default:
            print("default")

        }
    }
    
    func handleSetting(){
        switchVIP ? updataInfo(type: "1") : updataInfo(type: "0")
        UserDefaults.standard.setValue(switchVIP, forKey: "kWhetherVip")
        UserDefaults.standard.synchronize()
        NotificationCenter.default.post(name: Notification.Name("refreshVip"), object: nil)
    }
    
    
    func updataInfo(type: String){
        RequestManager().postWithJson("https://backend.aichattings.com/api/v2/user/updateSubscribe", parameters: ["ep_user_id":UserDefaults.standard.string(forKey: "ep_user_id") ?? "1",
                                                                                                                 "type":type,]) { result in
            
            switch result {
            case .success(let success):
                print("成功")
            case .failure(let failure):
                print("失败")
            }
        }
    }
}

extension SettingViewModel {
    func logShowSetting() {
        Analytics.logEvent("settings_page_show", parameters: nil)

    }
    func logShareClick() {
        Analytics.logEvent("settings_share", parameters: nil)

    }
    func logRateClick() {
        Analytics.logEvent("settings_rate", parameters: nil)

    }
}
